//
//  CollectViewController.m
//  谈诗说词
//
//  Created by dllo on 15/6/16.
//  Copyright (c) 2015年 lanou3g.com蓝鸥科技. All rights reserved.
//

#import "CollectViewController.h"
#import "ListCell.h"
#import "PoemModel.h"
#import "PomeDetailController.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@interface CollectViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView * table;

@property (nonatomic, retain) NSMutableArray * array;

@end

@implementation CollectViewController

- (void)dealloc
{
    
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0.32 * WIDTH, 0.08 * WIDTH)];
    title.text = @"我的收藏";
    title.font = [UIFont fontWithName:@"-" size:28];
    self.navigationItem.titleView = title;
    
    
    [self createTable];
    
    // 注册消息中心
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadTable) name:@"reloadTable" object:nil];
}

- (void)reloadTable
{
    [self.table reloadData];
}

- (void)createTable
{
    UIImageView *background = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64)];
    [background setImage:[UIImage imageNamed:@"背景.png"]];
    [self.view  addSubview:background];
    
    self.table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.table setBackgroundColor:[UIColor clearColor]];
    self.table.alpha = 0.7;
    [self.view addSubview:self.table];

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    NSArray * arr = [defaults objectForKey:@"collectArray"];
    self.array = [NSMutableArray arrayWithArray:arr];
    
    return self.array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //PoemModel *model = [self.array objectAtIndex:indexPath.row];
    ListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[ListCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    NSMutableDictionary *dictionary = [self.array objectAtIndex:indexPath.row];
    PoemModel *model = [[PoemModel alloc]init];
    [model setValuesForKeysWithDictionary:dictionary];
    cell.title.text = model.name;
    NSArray *array = [model.content componentsSeparatedByString:@"。"];
    NSString *content = [NSString stringWithFormat:@"%@。", [array firstObject]];
    NSDictionary *dic = @{NSFontAttributeName:[UIFont fontWithName:@"X-" size:19]};
    self.rect = [content boundingRectWithSize:CGSizeMake(WIDTH - 0.14 * WIDTH, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    cell.pome.frame = CGRectMake(0.07 * WIDTH, 0.22 * WIDTH, 0.86 * WIDTH, self.rect.size.height);
    cell.pome.text = content;
    cell.backgroundColor = [UIColor clearColor];
    
    cell.author.text = [NSString stringWithFormat:@"/%@/%@", model.dynasty_name, model.poet_name];
    return cell;
}

#pragma mark - cell自适应
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.rect.size.height + 0.26 * WIDTH;
}

#pragma mark - cell点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableDictionary *dictionary = [self.array objectAtIndex:indexPath.row];
    PoemModel *model = [[PoemModel alloc]init];
    [model setValuesForKeysWithDictionary:dictionary];
    PomeDetailController *detail = [[PomeDetailController alloc]init];
    detail.model = model;
//    detail.pomeTitle = model.name;
//    detail.author = [NSString stringWithFormat:@"/%@/%@", model.dynasty_name, model.poet_name];
//    detail.pome = model.content;
    CATransition *trans = [CATransition animation];
    [self.navigationController pushViewController:detail animated:NO];
    trans.type = @"rippleEffect";
    trans.duration = 0.8f;
    [self.navigationController.view.layer addAnimation:trans forKey:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
